%% This is Team A's Strategy. Team A : Rahil and Rishabh. See more discription at below
%team_a = strategy_team_a(team_a(:,1),team_b(:,1),score_team_a,loopcount)
function [team_a] = strategy_team_a(A,B,S,L)

    persistent param = [1;zeros(12,1)];
    persistent theta = zeros(13,1);
    persistent scorerecord = 0;
    persistent score = 0;
    if L>1,
        score(L-1) = S-scorerecord;
    end
        scorerecord = S;

if (L>15),
    theta = improvetheta(param,theta,score,L);
end

[param(:,L)] = createparam(A,B,theta,L);

team_a = [param(5:7,L)' param(11:13,L)'];

end

%score(loopcount,1) = createscore(score_team_a,loopcount)
%% createscore: gives the score gained in previous loopcount i.e. dependent parameter
% function [outputs] = createscore(S,L)
%     if (L>1),
%         outputs = S(L,1)-S(L-1,1);
%     else
%         outputs = 0;
%     end

% end


%theta(:,loopcount) = improvetheta(param,theta(:,loopcount-1),score(loopcount-1),loopcount);
%% improvetheta: takes the theta of previous loopcount and improves it with the help of score of previous count
function [outputs] = improvetheta(X,T,S,L)

    LR = 0.001;
    maxLR=100;
    improvement = 100;

    while (improvement>=10^(-10) | improvement<0),

        temp = 0;
        for j=1:13,     % summation part of theta improvement and costfunction
            for i=1:(L-1),
                temp(j) = temp(j)+((T'*X(:,i))-S)*(X(j,i));
            end
        end

        cost = 0;
        for i=1:(L-1),
            cost = cost+(((T'*X(:,i))-score)^2);
        end

        finalcost = (1/2*(L-1))*cost;

        if (improvement>0),
            T = T-(LR*(1/(L-1))*temp);
            if ((LR+0.01)<maxLR),
                LR = LR+0.01;
            end
        elseif (improvement<0),
            maxLR = LR;
            LR = LR-0.005;
        end

        cost = 0;
        for i=1:(L-1),
            cost = cost+(((T'*X(:,i))-score)^2);
        end

        improvement = finalcost - ((1/2*(L-1))*cost);
    end

    outputs = T;

end

%[param(:,loopcount),scoreexpected] = createparam(team_a(:,1),team_b(:,1),theta(:,loopcount),loopcount) % team_a(:,1) is the postion where we actually gone
%% createparam: outputs the parameters for current loopcount
function [outputs] = createparam(A,B,T,L)
    
    outputs = 1;
    outputs(8:10) = B;
    outputs(2:4) = A;
    flag = 0;
    se = 0;
    bestcomb = 0;

    if (L<=15),
        outputs(5:7) = [floor(rand(3,1)*100)];
        outputs(11:13) = [floor(rand(3,1)*100)];
    else
        for i=1:100,
            for j=1:100,
                for k=1:100,
                    for l=1:100,
                        for m=1:100,
                            for n=1:100,
                                outputs(5:7,11:13) = [i j k l m n];
                                score = T'*outputs;
                                if (score>=se),
                                    se = score;
                                    bestcomb = [i j k l m n];
                                end
                            end
                        end
                    end
                end
            end
        end
        outputs(5:7,11:13) = bestcomb;
    end

end

% Our hypothesis is score = theta(:,loopcount)'*param(:,loopcount)
% pos_ours = 2:4;       % our current position
% dest_ours = 5:7;      % destination where we will go after analysis
% pos_enemy = 8:10;     % last known position of enemy i.e. position of enemy from last turn
% dest_shoot_ours = 11:13;      % point where we will shoot after analysis